package com.auto.uhoo.ui.base;

import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Window;

import com.auto.uhoo.R;
import com.auto.uhoo.tools.Jumper;
import com.auto.uhoo.tools.LOG;

import butterknife.ButterKnife;

public abstract class BaseActivity extends AppCompatActivity {

    private final String TAG = BaseActivity.class.getSimpleName();

    private BaseFragment currentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_base);
        ButterKnife.bind(this);

        if (!TextUtils.isEmpty(getIntent().getStringExtra(Jumper.KEY_TARGET_FRAGMENT))){
            showFragment(getIntent());
        }
    }

    public void showFragment(Intent intent){
        String targetFragment = intent.getStringExtra(Jumper.KEY_TARGET_FRAGMENT);
        try {
            currentFragment = (BaseFragment)Class.forName(targetFragment).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (currentFragment == null){
            LOG.e(TAG, "target fragment error");
            return;
        }
        boolean backStack = intent.getBooleanExtra(Jumper.KEY_BACK_STACK, false);
        Bundle bundle = intent.getExtras();
        if (bundle != null)
            currentFragment.setArguments(bundle);

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.app_base, currentFragment, this.getClass().getName());
//        if (backStack)
//            ft.addToBackStack(targetFragment);

        ft.commitAllowingStateLoss();
    }

}
